package com.hp.web.controller.tool;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hp.common.core.controller.BaseController;
import com.hp.system.domain.IdentifyInfo;
import com.hp.system.domain.Info;
import com.hp.system.domain.Response_AlarmInfoPlate;
import com.hp.system.domain.Timeval;
import com.hp.system.service.IdentifyInfoServce;
import com.hp.system.service.PassInfoService;
import com.hp.system.utils.JSONInfoUtil;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;


@RestController
@RequestMapping("/tool/identify")
public class IdentifyController extends BaseController {
    @Autowired
    private IdentifyInfoServce identifyInfoServce;

    @Autowired
    private PassInfoService passInfoService;

    private String prefix = "tool/identify";

//    @RequiresPermissions("tool:identify:view")
    @GetMapping()
    public String identify(){
        return prefix+"/info";
    }

    /**
     * 车辆进入识别接口
     * @param info
     * @return
     * @throws Exception
     */
    @PostMapping("/info")
    @ResponseBody
    public Response_AlarmInfoPlate identify(@RequestBody String info) throws Exception{
        Info myinfo = JSON.parseObject(info,Info.class);
        Timeval timeval = myinfo.getAlarmInfoPlate().getResult().getPlateResult().getTimeStamp().getTimeval();
        IdentifyInfo identifyInfo = new IdentifyInfo();
        identifyInfo.setIdentifydate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(JSONInfoUtil.getTime(timeval)));
        identifyInfo.setLicense(myinfo.getAlarmInfoPlate().getResult().getPlateResult().getLicense());
        identifyInfo.setSerialno(myinfo.getAlarmInfoPlate().getSerialno());
        identifyInfo.setColortype(myinfo.getAlarmInfoPlate().getResult().getPlateResult().getColorType());
        identifyInfo.setIsdeal(new Byte("2"));
        if (identifyInfo.getLicense().equals("_无_")){
            return new Response_AlarmInfoPlate("no",myinfo.getAlarmInfoPlate().getResult().getPlateResult().getPlateid()) ;
        }else {
            identifyInfoServce.addInfo(identifyInfo);
            return new Response_AlarmInfoPlate("no",myinfo.getAlarmInfoPlate().getResult().getPlateResult().getPlateid()) ;
        }
    }

    /**
     * 车辆出去识别接口
     * @param info
     * @return
     * @throws Exception
     */
    @PostMapping("/out")
    @ResponseBody
    public Response_AlarmInfoPlate carOut(@RequestBody String info) throws Exception{
        Info myinfo = JSON.parseObject(info,Info.class);
        Timeval timeval = myinfo.getAlarmInfoPlate().getResult().getPlateResult().getTimeStamp().getTimeval();
        IdentifyInfo identifyInfo = new IdentifyInfo();
        identifyInfo.setIdentifydate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(JSONInfoUtil.getTime(timeval)));
        identifyInfo.setLicense(myinfo.getAlarmInfoPlate().getResult().getPlateResult().getLicense());
        identifyInfo.setSerialno(myinfo.getAlarmInfoPlate().getSerialno());
        identifyInfo.setColortype(myinfo.getAlarmInfoPlate().getResult().getPlateResult().getColorType());
        identifyInfo.setIsdeal(new Byte("2"));
        if (identifyInfo.getLicense().equals("_无_")){
            return new Response_AlarmInfoPlate("no",myinfo.getAlarmInfoPlate().getResult().getPlateResult().getPlateid()) ;
        }else {
            int result = passInfoService.carOutInfoDeal(identifyInfo);
            if (result==1)
                return new Response_AlarmInfoPlate("ok",myinfo.getAlarmInfoPlate().getResult().getPlateResult().getPlateid()) ;
            else
                return new Response_AlarmInfoPlate("no",myinfo.getAlarmInfoPlate().getResult().getPlateResult().getPlateid()) ;
        }
    }
    @PostMapping("/test")
    @ResponseBody
    public Response_AlarmInfoPlate test(@RequestBody String test){
        System.out.println(test);
        Response_AlarmInfoPlate result =  new Response_AlarmInfoPlate();
        result.setInfo("no");
        return result ;
    }

//    @PostMapping("/pop")
//    public String pop(@RequestBody String info,ModelMap mmap){
//        System.out.println("得到信息"+info);
//        IdentifyInfo identifyInfo = JSON.parseObject(info,IdentifyInfo.class);
//        mmap.put("identifyInfo",identifyInfo);
//        return prefix+"/edit";
//    }
}
